package com.lili.controller;


import com.lili.config.Constant;
import com.lili.controller.request.XasUserRequest;
import com.lili.exception.QhException;
import com.lili.exception.QhUserErrors;
import com.lili.filter.JwtHelper;
import com.lili.po.XasUser;
import com.lili.service.UserService;
import com.lili.vo.ResponseResult;
import com.lili.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/manager")
public class LoginController {
    @Autowired
    UserService userService;
    @Autowired
    Constant constant;


    @PostMapping("/login")
    public ResponseResult<UserVo> login(@RequestBody XasUserRequest userRequest) {
        ResponseResult<UserVo> responseResult = new ResponseResult();
        XasUser user = userService.findUserByLoginName(userRequest.getLoginName());
        if (user == null) {
            throw new QhException(QhUserErrors.USER_ERROR);
        }
        SimpleHash password = new SimpleHash("MD5", userRequest.getPassword());
        if (!StringUtils.equals(password.toString(), user.getPassword())) {
            throw new QhException(QhUserErrors.USER_ERROR);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        String jwt = JwtHelper.createJWT(user.getUid().toString(), user.getRid().toString(), null, null, 18000000l, JwtHelper.SIGNATURE);
        userVo.setToken(jwt);
        responseResult.setData(userVo);
        return responseResult;
    }


    @RequestMapping("/overTime")
    public ResponseResult overTime() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(-1);
        responseResult.setMsg("登录超时");
        return responseResult;
    }


    @RequestMapping("/tokenIsNull")
    public ResponseResult tokenIsNull() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(-1);
        responseResult.setMsg("token为空");
        return responseResult;
    }

    @RequestMapping("/tokenValidate")
    public ResponseResult tokenValidate() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(-1);
        responseResult.setMsg("token验证失败");
        return responseResult;
    }
}
